package com.tarena.lbs.coupon.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.api.pojo.param.UserCouponsParam;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.po.UserCouponsPO;
import com.tarena.lbs.coupon.pojo.query.CouponQuery;
import com.tarena.lbs.coupon.pojo.query.CouponStockQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import com.tarena.lbs.coupon.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CounponServiceImpl implements CouponService {
    @Autowired
    private CouponRepository couponRepository;

    @Override
    public PageResult<CouponVO> pageList(CouponQuery couponQuery) {
        log.info("控制层传递的查询当前商家优惠券分页列表{}", couponQuery);
        PageResult<CouponVO> pageResult = new PageResult<>(couponQuery.getPageSize(), couponQuery.getPageNo());
        PageInfo<CouponPO> pageInfo = getPageCounpon(couponQuery);
        pageResult.setTotal(pageInfo.getTotal());
        List<CouponPO> pos = pageInfo.getList();
        List<CouponVO> vos = new ArrayList<>();
        if (pos != null && pos.size() > 0) {
            vos = pos.stream().map(po -> {
                CouponVO vo = new CouponVO();
                BeanUtils.copyProperties(po, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setObjects(vos);
        return pageResult;
    }

    @Override
    public void addCoupon(CouponParam couponParam) {
        log.info("控制层传递的添加优惠券{}", couponParam);
        CouponPO po=new CouponPO();
        BeanUtils.copyProperties(couponParam,po);
        po.setEnableStatus(1);
        po.setStatus(1);
        po.setStartDate(new Date());
        po.setEndDate(new Date());
        po.setCreateAt(new Date());
        po.setUpdateAt(new Date());
        couponRepository.addCoupon(po);
    }

    @Override
    public List<CouponVO> detail(Integer id) {
        log.info("查询优惠券详情{}", id);
        CouponPO po = couponRepository.detail(id);
        if(po==null){
            return null;
        }
        CouponVO vo = new CouponVO();
        BeanUtils.copyProperties(po, vo);
        return List.of(vo);
    }

    @Override
    public void receiveCoupon(UserCouponsParam param) {
        log.info("用户领取优惠券{}", param);
        UserCouponsPO po=new UserCouponsPO();
        BeanUtils.copyProperties(param,po);
        po.setStatus(1);
        po.setCreateAt(new Date());

        couponRepository.addUserCoupon(po);
    }

    @Override
    public List<UserCouponsVO> receiveList(UserCouponQuery couponQuery) {
        log.info("用户领取优惠券列表{}", couponQuery);
        List<UserCouponsVO> pos = couponRepository.receiveList(couponQuery);
        return pos;
    }


    private PageInfo<CouponPO> getPageCounpon(CouponQuery couponQuery) {
        PageHelper.startPage(couponQuery.getPageNo(), couponQuery.getPageSize());
        log.info("查询优惠券列表页数{}，{}", couponQuery.getPageNo(), couponQuery.getPageSize());
        log.info("查询优惠券列表参数{}", couponQuery);
        List<CouponPO> pos=couponRepository.list(couponQuery);
        return new PageInfo<>(pos);
    }
}
